import { computed, ref } from 'vue'
import { defineStore } from 'pinia'
import { head, uniq } from 'lodash-es'
import type { UserInfo } from '@/core/interfaces/user'
import { AuthEnum } from '@/core/enums/auth'
import { useLocalStorage } from '@vueuse/core'
import { StorageEnum } from '@/core/enums/shared'
import { serializer } from '@/utils'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
const { SALES, AGENT, AGENT_BOSS, CUSTOMER, OPERATION } = AuthEnum

export const useMainStore = defineStore('Main', () => {
  const userInfo = ref<UserInfo['content'] | null>(null)
  const token = useLocalStorage<string | null>(StorageEnum.TOKEN, null, {
    serializer
  })
  const cibaotaoc = useLocalStorage<number>(StorageEnum.CIBAOTAOC, 0)
  const bansdsx = ref(7)
  const pricecibao = ref(0)
  const giftNumber1 = ref(0)
  const giftNumber2 = ref(0)
  const zhibianhs = ref(0)
  const zhibianh = ref(0)
  // const cibaotaoc = ref(0) //精准词词包套餐切换的时候的一个校验
  const zzbwOrderNod = ref('') //精准词词包套餐切换的时候的一个校验
  const registerRouteFresh = ref(true)
  const siderBreakpoint = ref(true)
  const keepName = ref(['Promotion', 'PricePackage', 'PriceManager', 'OrderManager'])

  const roles = computed(() => {
    const roleList = uniq(userInfo.value?.role_ids ?? [])
    roleList.sort()
    // 去重复
    return roleList
  })
  const userId = computed(() => {
    return userInfo.value?.id ?? 0
  })

  const customerId = computed(() => {
    return roles.value.some((role) => CUSTOMER === role) ? userInfo.value?.customer_id : 0
  })

  const sellerId = computed(() => {
    return head(roles.value) === SALES ? userId.value : 0
  })

  const proxyId = computed(() => {
    return roles.value.some((role) => [AGENT, AGENT_BOSS].includes(role)) ? (userInfo.value?.proxy_id ?? 0) : 0
  })

  const userMobile = computed(() => {
    return userInfo.value?.mobile
  })

  const isOperation = computed(() => {
    return roles.value.some((role) => OPERATION === role)
  })

  function setToken(t: string) {
    token.value = t
  }

  function banbes(t: number) {
    bansdsx.value = t
  }
  function zhibainhsds(t: number) {
    zhibianh.value = t
  }
  function zhibainhsdsd(t: number) {
    zhibianhs.value = t
  }

  function cibaotaocs(t: number) {
    cibaotaoc.value = t
  }
  function zzbwOrderNods(t: string) {
    zzbwOrderNod.value = t
  }

  function pricecibaos(t: number) {
    pricecibao.value = t
  }
  function removeToken() {
    token.value = null
  }

  function setUserInfo(data: UserInfo['content']) {
    userInfo.value = data
  }

  function setRegisterRouteFresh(fresh: boolean) {
    registerRouteFresh.value = fresh
  }

  function setBreakpoint(val: boolean) {
    siderBreakpoint.value = val
  }

  function setKeepName(name: string) {
    if (keepName.value.indexOf(name) < 0) {
      keepName.value.push(name)
    }
  }

  function removeKeepName(name: string) {
    const i = keepName.value.indexOf(name)
    if (i > -1) {
      keepName.value.splice(i, 1)
    }
  }

  function removeUserInfo() {
    userInfo.value = null
  }

  return {
    zhibainhsdsd,
    zhibianhs,
    pricecibaos,
    pricecibao,
    bansdsx,
    cibaotaocs,
    cibaotaoc,
    zhibianh,
    userInfo,
    registerRouteFresh,
    siderBreakpoint,
    keepName,
    roles,
    userId,
    customerId,
    sellerId,
    proxyId,
    userMobile,
    token,
    setToken,
    banbes,
    zhibainhsds,
    removeToken,
    isOperation,
    setUserInfo,
    setRegisterRouteFresh,
    setBreakpoint,
    setKeepName,
    removeKeepName,
    removeUserInfo,
    zzbwOrderNods,
    zzbwOrderNod
  }
})
